<?php

namespace App\Services;

use App\Caches\IpToolCache;
use App\Models\IpAddress;
use YQ\YqCurl;

class IpToolService extends ServiceBase
{
    public function get($ip)
    {
        return $this->bilibiliGeoIp($ip);
    }

    public function getAndCreate($ip)
    {
        $class = IpAddress::where('ip', $ip)->first();
        if ($class) {
            return $class->toArray();
        }

        $info = $this->aliyunHuaChenGeoIp($ip);
        $class = new IpAddress;
        $class->create_time = time();
        $class->ip = $ip;
        $class->country_code = $info['country_code'];
        $class->country = $info['country'];
        $class->region = $info['region'];
        $class->province = $info['province'];
        $class->city = $info['city'];
        $class->isp = $info['isp'];
        $class->save();

        return $class->toArray();
    }

    public function aliyunHuaChenGeoIp($ip)
    {
        $url     = "https://api01.aliyun.venuscn.com/ip?ip=" . $ip;
        $appcode = "a71fd910f7ae4bd392d854c06cbb1623";

        $headers = [];
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $res   = YqCurl::curl($url, false, 0, 1, 3, false, $headers);
        if ($res) {
            $ret = json_decode($res, true);
            if ($ret['ret'] == 200) {
                $data = $ret['data'];
                return [
                    'ip'           => $ip,
                    'country_code' => $data['country_id'], // 国家代号
                    'country'      => $data['country'], // 国家
                    'region'       => $data['area'], // 区域
                    'province'     => $data['region'], // 省份
                    'city'         => $data['city'], // 市
                    'isp'          => $data['isp'], // 运营商
                ];
            }
        }

        return [
            'ip'           => $ip,
            'country_code' => '',
            'country'      => '',
            'region'       => '',
            'province'     => '',
            'city'         => '',
            'isp'          => '',
        ];
    }

    // B站查询
    // https://socialsisteryi.github.io/bilibili-API-collect/docs/clientinfo/ip.html
    public function bilibiliGeoIp($ip, $urltype = 1)
    {
        if ($urltype == 1) {
            $url = "https://api.live.bilibili.com/ip_service/v1/ip_service/get_ip_addr?ip=" . $ip;
        } else {
            $url = "https://api.live.bilibili.com/client/v1/Ip/getInfoNew?ip=" . $ip;
        }

        $res = YqCurl::curl($url, false, 0, 1, 3, false);
        if ($res) {
            $ret = json_decode($res, true);
            if ($ret['code'] === 0) {
                $data = $ret['data'];
                return [
                    'ip'           => $ip,
                    'country_code' => '', // 国家代号
                    'country'      => $data['country'], // 国家
                    'region'       => '', // 区域
                    'province'     => $data['province'], // 省份
                    'city'         => $data['city'], // 市
                    'isp'          => $data['isp'], // 运营商
                ];
            }
        }

        return [
            'ip'           => $ip,
            'country_code' => '',
            'country'      => '',
            'region'       => '',
            'province'     => '',
            'city'         => '',
            'isp'          => '',
        ];
    }
}
